package com.itheima.springbootwebbtalis.controller;

import com.itheima.springbootwebbtalis.pojo.Emp;
import com.itheima.springbootwebbtalis.pojo.Result;
import com.itheima.springbootwebbtalis.service.EmpService;
import com.itheima.springbootwebbtalis.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class loginController {
    @Autowired
    private EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("登录成功");
        Emp result =empService.login(emp);
        Map map = new HashMap();
        map.put("id",result.getId());
        map.put("username",result.getUsername());
        map.put("name",result.getName());
        // 登录成功，颁发令牌
        String jwt = JwtUtils.generateJwt(map); // 生成jwt令牌
        map.put("token",jwt); // 颁发令牌
        log.info("生成jwt令牌：{}",jwt);
        return Result.success(map);
    }
}
